package org.example.springboot.controller;

import org.example.springboot.common.Result;
import org.example.springboot.entity.Classification;
import org.example.springboot.service.ClassificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/classification")
public class ClassificationController {

    @Autowired
    ClassificationService service;

    @GetMapping("/page")
    public Result getClassificationsByPage(@RequestParam("pageNum") int pageNum,
                                 @RequestParam("pageSize") int pageSize,
                                 @RequestParam(value="course",required = false) String course) {
        List<Classification> classificationList = service.getClassificationsByPage(pageNum, pageSize,course);



        int total = service.getClassificationCount(course);

        Map<String, Object> data = new HashMap<>();
        data.put("list", classificationList);  // 用户列表
        data.put("total", total);    // 总记录数

        return Result.success(data);
    }

    @DeleteMapping("/del/{course}")
    public Result delClassification(@PathVariable String course) {
        service.deleteClassificationByAccount(course);
        return Result.success("删除成功");
    }

    @PostMapping("/add")
    public Result addClassification(@RequestBody Classification classification) {

        Classification selectClassification = service.getClassificationByAccount(classification.getCourse());
        if(selectClassification!=null)
            return Result.error("用户已存在");

        service.insertClassification(classification);
        return Result.success("成功添加");
    }

    @PostMapping("/update")
    public Result updateClassification(@RequestBody Classification classification) {
        service.updateClassification(classification);
        return Result.success();
    }

    @GetMapping("/selectAll")
    public Result selectAll() {
       return Result.success(service.selectAllClassification());
    }

}
